package org.light.reflecttable.facade;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.light.reflecttable.domain.Field;
import org.light.reflecttable.serviceimpl.TableServiceImpl;

import net.sf.json.JSONObject;

public class UpdateDbSettingsFacade extends HttpServlet{
	private static final long serialVersionUID = -6575492756745777296L;

	protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		response.setContentType("application/json;charset=UTF-8");
		String dbType = request.getParameter("dbType");
		String dbLink = request.getParameter("dbLink");
		String dbHost = request.getParameter("dbHost");
		String dbPort = request.getParameter("dbPort");
		String dbName = request.getParameter("dbName");
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		int topn = Integer.parseInt(request.getParameter("topn"));
		TableServiceImpl.dbType = dbType;
		TableServiceImpl.dbLink = dbLink;
		TableServiceImpl.dbHost = dbHost;
		TableServiceImpl.dbPort = dbPort;
		TableServiceImpl.dbName = dbName;
		TableServiceImpl.userName = userName;
		TableServiceImpl.password = password;
		TableServiceImpl.topn = topn;
		
		Map<String,Object> result = new TreeMap<String,Object>();
		PrintWriter out = response.getWriter();
		result.put("success",true);
		result.put("rows",null);
		out.print(JSONObject.fromObject(result).toString());
	}

	public String getServletInfo(){
		return "Powered by Mind Rules.";
	}

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		processRequest(request, response);
	}

}
